#include ".\configviewmanager.h"

ConfigViewManager::ConfigViewManager(QStackedWidget *stackedWidget, QObject *parent)
    : QObject(parent), m_stackedWidget(stackedWidget)
{
    initViews();
}

ConfigViewManager::~ConfigViewManager()
{
    // 视图对象由ConfigViewManager负责删除（因为是父对象的子对象）
}

void ConfigViewManager::initViews()
{
    m_groupView = new GroupConfigView(this);
    m_propertyView = new PropertyConfigView(this);
    m_jsonView = new JsonConfigView(this);
    m_cardView = new CardConfigView(this);

    m_stackedWidget->addWidget(m_groupView->viewWidget());
    m_stackedWidget->addWidget(m_propertyView->viewWidget());
    m_stackedWidget->addWidget(m_jsonView->viewWidget());
    m_stackedWidget->addWidget(m_cardView->viewWidget());

    // 默认显示分组视图
    setCurrentView(GroupView);
}

void ConfigViewManager::setCurrentView(ConfigViewType type)
{
    if (!m_stackedWidget) return;

    switch (type) {
    case GroupView:
        m_stackedWidget->setCurrentWidget(m_groupView->viewWidget());
        m_groupView->updateConfig(m_currentConfig);
        break;
    case PropertyView:
        m_stackedWidget->setCurrentWidget(m_propertyView->viewWidget());
        m_propertyView->updateConfig(m_currentConfig);
        break;
    case JsonView:
        m_stackedWidget->setCurrentWidget(m_jsonView->viewWidget());
        m_jsonView->updateConfig(m_currentConfig);
        break;
    case CardView:
        m_stackedWidget->setCurrentWidget(m_cardView->viewWidget());
        m_cardView->updateConfig(m_currentConfig);
        break;
    }
}

void ConfigViewManager::updateConfig(const QJsonArray &configArray)
{
    qDebug() << __FILE__ << " : " << __FUNCTION__;
    m_currentConfig = configArray;

    // 更新当前视图
    if (m_stackedWidget->currentWidget() == m_groupView->viewWidget()) {
        m_groupView->updateConfig(m_currentConfig);
    } else if (m_stackedWidget->currentWidget() == m_propertyView->viewWidget()) {
        m_propertyView->updateConfig(m_currentConfig);
    } else if (m_stackedWidget->currentWidget() == m_jsonView->viewWidget()) {
        m_jsonView->updateConfig(m_currentConfig);
    } else if (m_stackedWidget->currentWidget() == m_cardView->viewWidget()) {
        m_cardView->updateConfig(m_currentConfig);
    }

    emit configModified();
}

QJsonArray ConfigViewManager::getModifiedConfig() const
{
    // 返回当前视图修改后的配置
    if (m_stackedWidget->currentWidget() == m_groupView->viewWidget()) {
        return m_groupView->getModifiedConfig();
    } else if (m_stackedWidget->currentWidget() == m_propertyView->viewWidget()) {
        return m_propertyView->getModifiedConfig();
    } else if (m_stackedWidget->currentWidget() == m_jsonView->viewWidget()) {
        return m_jsonView->getModifiedConfig();
    } else if (m_stackedWidget->currentWidget() == m_cardView->viewWidget()) {
        return m_cardView->getModifiedConfig();
    }
    return m_currentConfig;
}

void ConfigViewManager::resetToOriginal()
{
    if (m_stackedWidget->currentWidget() == m_groupView->viewWidget()) {
        m_groupView->resetToOriginal();
    } else if (m_stackedWidget->currentWidget() == m_propertyView->viewWidget()) {
        m_propertyView->resetToOriginal();
    } else if (m_stackedWidget->currentWidget() == m_jsonView->viewWidget()) {
        m_jsonView->resetToOriginal();
    } else if (m_stackedWidget->currentWidget() == m_cardView->viewWidget()) {
        m_cardView->resetToOriginal();
    }
}

void ConfigViewManager::applyEdits()
{
    if (m_stackedWidget->currentWidget() == m_groupView->viewWidget()) {
        m_groupView->applyEdits();
    } else if (m_stackedWidget->currentWidget() == m_propertyView->viewWidget()) {
        // 属性视图通常是只读的
    } else if (m_stackedWidget->currentWidget() == m_jsonView->viewWidget()) {
        m_jsonView->applyEdits();
    } else if (m_stackedWidget->currentWidget() == m_cardView->viewWidget()) {
        // 卡片视图通常是只读的
    }
}

void ConfigViewManager::sendCurrentConfig()
{
    // 应用所有编辑
    applyEdits();

    // 获取当前视图的修改后配置
    QJsonArray configToSend = getModifiedConfig();

    // 发出发送请求信号
    emit sendConfigRequested(configToSend);
}
